//
//  CodeLoginController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/1.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "CodeLoginController.h"
#import "LMCountDownButton.h"
#import "AccountLoginController.h"
#import "RegistViewController.h"
#import "UserAgreementViewController.h"
#import <WXApi.h>
@interface CodeLoginController ()
@property (weak, nonatomic) IBOutlet LMCountDownButton *codeButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UIButton *agreeButton;

@end

@implementation CodeLoginController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    LMCountDownButton *sendCodeButton = [[LMCountDownButton alloc] init];
    [sendCodeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
    [sendCodeButton setTitleColor:[UIColor colorWithHexString:@"#FF7321"] forState:(UIControlStateNormal)];
    sendCodeButton.titleLabel.font = [UIFont systemFontOfSize:15];
    sendCodeButton.frame = self.codeButton.bounds;
    [sendCodeButton addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    [self.codeButton addSubview:sendCodeButton];
    
    [Center addObserver:self selector:@selector(wechataction:) name:@"weiChatOK" object:nil];

}
// 获取验证码
-(void)click:(LMCountDownButton *)sender {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    //去除字符串中空格
    self.phoneTextField.text = [self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确电话号码");
        return;
    }
    NSDictionary *dic = @{@"phone":self.phoneTextField.text};
    
    [NetWorkTool POST:sendSms param:[dic mutableCopy] success:^(id dic) {
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [sender startCountDown];
        ShowToastWithText(@"验证码发送成功");
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
// 注册
- (IBAction)registeredButtonClick:(UIButton *)sender {
    RegistViewController * vc = [[RegistViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
// 账号登录
- (IBAction)accountButtonClick:(UIButton *)sender {
    AccountLoginController * vc = [[AccountLoginController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
// 立即登录
- (IBAction)loginNowButtonClick:(UIButton *)sender {
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = self.phoneTextField.text;
    param[@"code"] = self.codeTextField.text;
    //去除字符串中空格
    self.phoneTextField.text = [self.phoneTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:self.phoneTextField.text]) {
        ShowToastWithText(@"请输入正确的手机号");
        return;
    }
    
    if ([self.codeTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"验证码不能为空");
        return;
    }
    
    if (self.agreeButton.selected == NO) {
        ShowToastWithText(@"请阅读并同意用户协议及隐私政策");
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:userCodelogin param:param success:^(id dic) {
        NSString *userid = [[dic objectForKey:@"data"] objectForKey:@"id"];
        [USER_DEFAULT setObject:userid forKey:@"user_id"];
        TabBarViewController *tabbar = [[TabBarViewController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
// 微信登录
- (IBAction)weChatLoginButtonClick:(UIButton *)sender {
    // 想判断就判断 不想判断拉倒
//    if([WXApi isWXAppInstalled]){//判断用户是否已安装微信App
//
        SendAuthReq *req = [[SendAuthReq alloc] init];
        req.state = @"wx_oauth_authorization_state";//用于保持请求和回调的状态，授权请求或原样带回
        req.scope = @"snsapi_userinfo";//授权作用域：获取用户个人信息
        //唤起微信
        [WXApi sendReq:req];
//    }else{
//        ShowToastWithText(@"未安装微信应用或版本过低");
//    }

}
#pragma mark - 微信授权成功
-(void)wechataction:(NSNotification *)no{
    NSLog(@"%@",no.userInfo);
    NSDictionary * weChatUserInfoDic = no.userInfo;
    
    NSString *headimgurl = weChatUserInfoDic[@"headimgurl"];
    NSString *nickname = weChatUserInfoDic[@"nickname"];
    NSString *openid = weChatUserInfoDic[@"openid"];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:0];
    param[@"nickname"] = nickname;
    param[@"img"] = headimgurl;
    param[@"openId"] = openid;
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:wxAppLogin param:param success:^(id dic) {
        NSString *userid = [[dic objectForKey:@"data"] objectForKey:@"id"];
        [USER_DEFAULT setObject:userid forKey:@"user_id"];
        TabBarViewController *tabbar = [[TabBarViewController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:NO];
}
// 同意协议
- (IBAction)selectedButtonClick:(UIButton *)sender {
    self.agreeButton.selected = !self.agreeButton.selected;
}
// 用户协议
- (IBAction)userAgreementButtonClick:(UIButton *)sender {
    UserAgreementViewController * vc = [[UserAgreementViewController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@%@",HOST_LINK,UserProtocal];
    vc.titleString = @"用户协议";
    [self.navigationController pushViewController:vc animated:YES];
}
// 隐私政策
- (IBAction)privacyPolicyButtonClick:(UIButton *)sender {
    UserAgreementViewController * vc = [[UserAgreementViewController alloc] init];
    vc.urlString = [NSString stringWithFormat:@"%@%@",HOST_LINK,UserPolicy];
    vc.titleString = @"隐私政策";
    [self.navigationController pushViewController:vc animated:YES];
}
///判断是否为电话号码
- (BOOL)isMobileNumberOnly:(NSString *)mobileNum {
    NSString * MOBILE = @"^((10[0-9])|(11[0-9])|(12[0-9])|(13[0-9])|(14[0-9])|(15[0-9])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    if ([regextestmobile evaluateWithObject:mobileNum] == YES) {
        return YES;
    } else {
        return NO;
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"weiChatOK" object:self];
}
@end
